package com.webside.wap.search.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.webside.wap.common.model.BSeries;
import com.webside.wap.common.model.Bdictionary;
import com.webside.wap.common.service.BSeriesService;
import com.webside.wap.common.service.BdictionaryService;
import com.webside.wap.listener.BaseData;

import jodd.util.URLDecoder;

import com.alibaba.fastjson.JSON;
import com.webside.util.Common;
import com.webside.util.PageUtil;

@Controller
@RequestMapping(value = "/wap")
public class SearchController {
	@Autowired
	private BdictionaryService bdictionaryService;
	@Autowired
	private BSeriesService bSeriesService;

	@RequestMapping(value = "/search.html", method = { RequestMethod.GET, RequestMethod.POST })
	public String find(Model model, String code, String type, String name) {
		Bdictionary bdictionary = new Bdictionary();
		// 页面标题等信息
		bdictionary.setType("RESEARCH_SEO");
		model.addAttribute("bdictionarySPageInfo", bdictionaryService.queryByCodeAndTypeAsc(bdictionary, "sort"));
		// 图片根路径
		model.addAttribute("baseImgPath", BaseData.img_base_path);
		// 获取全部地区
		bdictionary.setType("AREA");
		model.addAttribute("address_list", bdictionaryService.queryByCodeAndTypeAsc(bdictionary, "sort"));
		// 获取全部年份
		bdictionary.setType("YEAR");
		model.addAttribute("year_list", bdictionaryService.queryByCodeAndTypeDesc(bdictionary, "code"));
		//测试
		//List<Bdictionary> li =  searchService.queryByCodeAndTypeDesc(bdictionary, "code");
		//for (Bdictionary ii : li) {
		//		System.out.println(ii);
		//}
		//转译name
		String a = null;
		if(name!=null){
				try {
					 a=new String(name.getBytes("ISO-8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
		}
		// 获取清晰度
		bdictionary.setType("VIDEO_CLARITY");
		model.addAttribute("clear_list", bdictionaryService.queryByCodeAndTypeAsc(bdictionary, "sort"));
		// 获取全部类型
		if (code==null||code=="") {
			code="MOVIE";
		}
		bdictionary.setCode(code);
		bdictionary.setType("DRAMA_TYPE");
		model.addAttribute("channels_list", bdictionaryService.queryByCodeAndTypeAsc(bdictionary, "sort"));
		model.addAttribute("type", type);
		System.out.println("传入的name是"+a);
		model.addAttribute("name1",a);
		return Common.BACKGROUND_PATH + "/wap/choose";
	}

	@RequestMapping(value = "/findTypes.html", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody List<Bdictionary> findTypesByCode(String code) {

		if (code == null) {
			code = bdictionaryService.queryByCodeAndTypeAsc(new Bdictionary("CHANNEL", null), "sort").get(0).getCode();
		}
		return bdictionaryService.queryByCode(code);
	}
	/**
	 * 条件筛选的controller
	 */
	@RequestMapping(value = "/searchBSeries.html", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody List<BSeries> searchByPage(Model model, String pSize, String pageNo, String sort, String name,String area,
			String channel, String type, String year,String videoClarity,String kind,HttpServletRequest request) {
		System.out.println("_______________");

		BSeries bSeries = new BSeries();
			bSeries.setName(name);
			bSeries.setYear(year);
			bSeries.setChannel(channel);
			bSeries.setType(type);
			bSeries.setKind(kind);
			bSeries.setVideoClaritys(videoClarity);
			bSeries.setArea(area);
			
		System.out.println(JSON.toJSON(bSeries));

		model.addAttribute("baseImgPath", BaseData.img_base_path);
		PageUtil pageUtil = new PageUtil();
		pageUtil.setPageNum(1);
		pageUtil.setPageSize(5);

		int count = bSeriesService.getProductsCount(bSeries);
		System.out.println("总条数:"+count);
		int totalPageCount;
		if (count > 8) {
			totalPageCount = count / 8;
			if (count % 8 == 0) {
				model.addAttribute("totalPageCount", totalPageCount);

			} else {
				model.addAttribute("totalPageCount", totalPageCount + 1);
			}
		} else {
			totalPageCount = 0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		if (sort == null) {
			sort = "publish_time";
		}
		model.addAttribute("baseImgPath", BaseData.img_base_path);
		System.out.println("总页数："+totalPageCount);
		List<BSeries> list = bSeriesService.findBSeriesByPageVo(bSeries, pageUtil, sort);
		if (list.size() < 8) {
			model.addAttribute("totalPageCount", list.size());
		}
		System.out.println("列表大小"+list.size());
		System.out.println("初始化电影列表全靠我");
		return list;
	}
	/**
	 * 分页controller	
	 */
	@RequestMapping(value = "/searchPage.html", method = { RequestMethod.GET, RequestMethod.POST })
	public String searchPage(Model model, String pSize, String pageNo, String sort, String name,String area,
			String channel, String type, String year,String videoClarity,HttpServletRequest request) {
		
		BSeries bSeries = new BSeries();
			bSeries.setName(name);
			bSeries.setYear(year);
			bSeries.setChannel(channel);
			bSeries.setType(type);
			bSeries.setVideoClaritys(videoClarity);
			bSeries.setArea(area);
		
		model.addAttribute("baseImgPath", BaseData.img_base_path);
		System.out.println("每页数据条数" + pSize);
		System.out.println("页码" + pageNo);
		System.out.println("排序依据" + sort);
		System.out.println("name="+name);
		System.out.println("year="+year);
		System.out.println("type="+type);
		System.out.println("清晰度="+videoClarity);
		System.out.println("地区="+area);
		PageUtil pageUtil = new PageUtil();
		int no = Integer.parseInt(pageNo);
		pageUtil.setPageNum(no);
		pageUtil.setPageSize(Integer.parseInt(pSize));
		System.out.println(pageUtil.getPageSize());
		int totalPageCount;
		int count = bSeriesService.getProductsCount(bSeries);
		if (count > 8) {
			totalPageCount = count/8;
			if (count % 8 == 0) {

			} else {
				totalPageCount+=1;
			}
		} else {
			totalPageCount = 0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		if(totalPageCount==no||totalPageCount<no){
			model.addAttribute("end","true");
		}else{
			model.addAttribute("end","false");
		}
		if (sort == null) {
			sort = "publish_time";
		}
		List<BSeries> list = bSeriesService.findBSeriesByPageVo(bSeries, pageUtil, sort);
		model.addAttribute("serieslist", list);
		System.out.println("数据共"+totalPageCount+"页");
		System.out.println("第几页："+no + "/n共几条：" + list.size());
		return Common.BACKGROUND_PATH + "/wap/serieslist";
	}

}
